package game.module.ddz.processor;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRound;
import game.module.ddz.logic.DdzDataManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageDdz.C2SDdzFanQiang;
import lua.LuaMessageDdz.PushDdzFanQiang;
import lua.LuaMessageDdz.PushDiZhuSelect;
import lua.LuaMessageDdz.S2CDdzFanQiang;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SDdzFanQiang.id, accessLimit = 500)
public class DdzFanQiangProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzFanQiangProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		C2SDdzFanQiang reqMsg = C2SDdzFanQiang.parse(request);
		int playerId = playingRole.getId();
		byte is_fan_qiang = reqMsg.is_fan_qiang;
		logger.info("ddz qiang dizhu,playerId={},is_fan_qiang={}", playerId, is_fan_qiang);
		// 房间是否存在
		DdzRound ddzRound = DdzDataManager.getInstance().getDdzRoom(playerId);
		if (ddzRound == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 房间类型是否正确
		if (ddzRound.getJiaoFenOrQiang() == 1) {
			S2ErrorCode respMsg = new S2ErrorCode(208);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 还没人抢地主
		if (ddzRound.getQiangDiZhuIndex() == null) {
			S2ErrorCode respMsg = new S2ErrorCode(210);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 地主已经产生
		if (ddzRound.getDiZhuIndex() != null) {
			S2ErrorCode respMsg = new S2ErrorCode(211);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 抢地主的人不能反抢
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		if (myIndex == ddzRound.getQiangDiZhuIndex()) {
			S2ErrorCode respMsg = new S2ErrorCode(212);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// do
		// 处理
		if (is_fan_qiang == 1) {
			sendResp(playingRole, ddzRound, playerId, is_fan_qiang);
			selectDiZhu(ddzRound, myIndex, (short) 4);
		} else {
			sendResp(playingRole, ddzRound, playerId, is_fan_qiang);
			synchronized (ddzRound) {
				// 是否没人反抢地主
				Set<Byte> cancelFanQiangSet = ddzRound.getCancelFanQiangSet();
				if (cancelFanQiangSet == null) {
					cancelFanQiangSet = new HashSet<>();
					ddzRound.setCancelFanQiangSet(cancelFanQiangSet);
				}
				cancelFanQiangSet.add((byte) myIndex);
				// 所有人都没有抢地主
				if (cancelFanQiangSet.size() >= ddzRound.getPlayerIds().size() - 1) {
					selectDiZhu(ddzRound, ddzRound.getQiangDiZhuIndex(), (short) 2);
				}
			}
		}
	}

	private void sendResp(PlayingRole playingRole, DdzRound ddzRound, int playerId, byte is_qiang) {
		S2CDdzFanQiang retMsg = new S2CDdzFanQiang();
		playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
		// push
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		PushDdzFanQiang pushMsg = new PushDdzFanQiang((byte) myIndex, is_qiang);
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	private void selectDiZhu(DdzRound ddzRound, int myIndex, short jiaoScore) {
		ddzRound.setDiZhuIndex(myIndex);
		ddzRound.setChuPaiIndex(myIndex);
		ddzRound.setRoundScore(jiaoScore);
		// 底牌移动到玩家牌上
		List<Byte> myCards = ddzRound.getCards().get(myIndex);
		myCards.addAll(ddzRound.getDi());
		// 推送消息
		PushDiZhuSelect pushDiZhuSelect = new PushDiZhuSelect((short) myIndex);
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushDiZhuSelect.build(pr.alloc()));
			}
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
